package com.dmall.algorithm.chapter03;

import java.util.Arrays;

public class Leetcode283V3 {
    public void moveZeroes(int[] nums) {
        int x = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (x != i) {
                    swap(nums, x++, i);
                } else {
                    x++;
                }
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void main(String[] args) {
        int[] nums = {0, 1, 0, 3, 12};
        Leetcode283V2 leetcode283 = new Leetcode283V2();
        leetcode283.moveZeroes(nums);
        Arrays.stream(nums).forEach(System.out::println);
    }
}
